package com.lx.gulimall.product.web;

import com.lx.gulimall.product.entity.CategoryEntity;
import com.lx.gulimall.product.service.CategoryService;
import com.lx.gulimall.product.vo.Catelog2Vo;
import org.redisson.api.RLock;
import org.redisson.api.RReadWriteLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.Banner;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author lixun Email:lx3027@126.com
 * @create 2021-07-20 10:59
 */
@Controller
public class IndexController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    @GetMapping({"/", "/index.html"})
    public String indexPage(Model model) {

        // todo 查出所有的一级分类
        List<CategoryEntity> categoryEntities = categoryService.getLevel1CategoryService();
        model.addAttribute("categorys", categoryEntities);
        return "index";
    }

    @ResponseBody
    @GetMapping("/index/catalog.json")
    public Map<String, List<Catelog2Vo>> getCatalogJson() {
        Map<String, List<Catelog2Vo>> catalogJson = categoryService.getCatalogJson();
        return catalogJson;
    }

    @ResponseBody
    @RequestMapping("/hello")
    public String hello() {
        // 1.获取一把锁，只要锁的名字一样就式同一把锁
        RLock lock = redissonClient.getLock("my-lock");
        // 2.加锁
        lock.lock(); // 阻塞式等待，默认加的锁都是30s时间。
        // 1)、锁自动续期，如果业务超长，运行时期自动给锁续上新的30s。不用担心业务时间长，锁自动过期被删掉
        // 2)、加锁的业务只要运行完成，就不会给当前锁续费，即使不手动解锁，锁默认在30s以后自动删除
        //        -->锁不释放就一直续时
        try {
            System.out.println("加锁成功:-->" + Thread.currentThread().getId());
            // 睡30s
            Thread.sleep(30000);
        } catch (Exception e) {

        } finally {
            lock.unlock();
            System.out.println("解锁成功:-->" + Thread.currentThread().getId());
        }
        return "hello";
    }


    // 保证一定能读到最后的数据，修改期间，写锁是一个排他锁（互斥锁、独享锁）。读锁是一个共享锁
    // 写锁没释放读就必须等待
    // 读 + 读 ： 相当于无锁，并发读，只会在redis中记录好，当前所有的读锁。他们都会同时加锁成功
    // 写 + 读 ：等待写锁释放
    // 写 + 写 ：阻塞方式
    // 读 + 写 ：有读锁，写需要等待
    // 只要有写的存在，都必须等待
    @GetMapping("/write")
    @ResponseBody
    public String writeValue(){

        RReadWriteLock readWriteLock = redissonClient.getReadWriteLock("rw-lock");
        RLock rLock = readWriteLock.writeLock();
        String s = null;
        try {
            // 改数据加锁 读数据加锁
            rLock.lock();
            s = UUID.randomUUID().toString();
            Thread.sleep(30000);
            stringRedisTemplate.opsForValue().set("writeValue", s);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            rLock.unlock();
        }
        return s;
    }

    @GetMapping("/read")
    @ResponseBody
    public String readValue(){
        RReadWriteLock readWriteLock = redissonClient.getReadWriteLock("rw-lock");
        RLock rLock = readWriteLock.readLock();
        rLock.lock();
        String writeValue = null;
        try {
            writeValue = stringRedisTemplate.opsForValue().get("writeValue");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            rLock.unlock();
        }
        return writeValue;
    }


}
